package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.BuildingManagement;
import com.example.mybatisplus.model.dto.PageDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.HousingInfrastructureService;
import com.example.mybatisplus.model.domain.HousingInfrastructure;

import java.io.Serializable;
import java.sql.Wrapper;


/**
 * 前端控制器
 *
 * @author ablbb
 * @version v1.0
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/api/housingInfrastructure")
public class HousingInfrastructureController {

    private final Logger logger = LoggerFactory.getLogger(HousingInfrastructureController.class);

    @Autowired
    private HousingInfrastructureService housingInfrastructureService;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        HousingInfrastructure housingInfrastructure = housingInfrastructureService.getById(id);
        return JsonResponse.success(housingInfrastructure);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        housingInfrastructureService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateHousingInfrastructure(HousingInfrastructure housingInfrastructure) throws Exception {
        housingInfrastructureService.updateById(housingInfrastructure);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建HousingInfrastructure
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(HousingInfrastructure housingInfrastructure) throws Exception {
        housingInfrastructureService.save(housingInfrastructure);
        return JsonResponse.success(null);
    }

    // 显示数据+分页（分页似乎没体现？数据太少了，之后再看看吧，视频都盘包浆了，总有细节出问题
    @GetMapping("pageList")
    public JsonResponse pageList(HousingInfrastructure housingInfrastructure, PageDTO dto) {
        Page<HousingInfrastructure> page = housingInfrastructureService.pageList(housingInfrastructure, dto);
        return JsonResponse.success(page);
    }

    // 删除，在一遍遍的改bug中，总算找到了一个能用的代码
    @GetMapping("removePark")
    public JsonResponse removePark(@RequestParam String parkArea) {
        Boolean b = housingInfrastructureService.removeById(parkArea);
        return JsonResponse.success(b);
    }

    // 修改园区
    @PostMapping("edit")
    public JsonResponse edit(@RequestBody HousingInfrastructure housingInfrastructure) {
        boolean b = housingInfrastructureService.updateById(housingInfrastructure);
        return JsonResponse.success(b);
    }

    // 添加园区
    @PostMapping("addPark")
    public JsonResponse add(@RequestBody HousingInfrastructure housingInfrastructure){
        boolean b=housingInfrastructureService.save(housingInfrastructure);
        return JsonResponse.success(b);
    }
}

